﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Group_LedgerGroup : System.Web.UI.Page
{

    Datautility DU = new Datautility();
    string strsql = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {            
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "addnew")
                {
                    PnlAddNew.Visible = true;
                    _BindParentGroup(ddlParentGroup);
                }
                else if (Request.QueryString["action"].ToString() == "view")
                {
                    PnlView.Visible = true;
                    _BindGridViewData();
                }
            }
        }
    }
    private void _SaveGroup(string strGid, string strGname, string strGAName, string strPid)
    {
        if (strGid != string.Empty && strGname != string.Empty && strPid != string.Empty)
        {
            if (_ValidateGroup(strGid,strGname) && _ValidateGroup1(strGid,strGAName))
            {
                try
                {
                    string strSector = string.Empty;
                    string strGroupTreeID = string.Empty;
                    strsql = "Select sector from ACC_GRPMAST where GID='" + strPid + "'";
                    DataTable DTSector = DU.getdata(strsql);
                    if (DTSector.Rows.Count > 0)
                    {
                        strSector = DTSector.Rows[0]["SECTOR"].ToString();
                    }
                    SqlParameter[] sparam ={
                                                new SqlParameter ("@GID",strPid),                                  
                                            };
                    DataTable DTGroupTreeId = DU.getdatausingprocedure("USP_ACC_CreateNewGroupTreeID", sparam);
                    if (DTGroupTreeId.Rows.Count > 0)
                    {
                        if (DTGroupTreeId.Rows[0][0].ToString() != string.Empty)
                        {
                            strGroupTreeID = DTGroupTreeId.Rows[0][0].ToString();
                        }
                    }
                    if (strGroupTreeID != string.Empty)
                    {
                        SqlParameter[] sparam1 ={
                                                new SqlParameter ("@GID",strGid),
                                                new SqlParameter ("@PID",strPid),
                                                new SqlParameter ("@GROUPTREEID",strGroupTreeID.ToString()),
                                                new SqlParameter ("@SECTOR",strSector.ToString()),
                                                new SqlParameter ("@GNAME",strGname),
                                                new SqlParameter ("@GANAME",strGAName),
                                                new SqlParameter ("@PREDEFINED","0"),
                                                new SqlParameter ("@BEHAVIOURHEAD","0"),
                                                new SqlParameter ("@CNAME",Session["USERNAME"].ToString())                                     
                                            };
                        int result = DU.executeprocedure("USP_ACC_SaveGroup", sparam1);
                        if (result > 0)
                        {
                            lblMsg.Attributes.Remove("class");
                            lblMsg.Attributes.Add("class", "true");
                            lblMsg.Text = "Group Updated Successfully...";
                            Reset_Control();
                            _BindGridViewData();
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblMsg.Attributes.Remove("class");
                    lblMsg.Attributes.Add("class", "false");
                    lblMsg.Text = ex.Message;
                }
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        _SaveGroup("-1", txtGroup.Text, txtAltGroup.Text, ddlParentGroup.SelectedValue);
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }

     private void _BindParentGroup(DropDownList ddl)
    {
        strsql = "SELECT GID,GNAME FROM ACC_GRPMAST ORDER BY GNAME ASC";
        DataTable DTParent = DU.getdata(strsql);
        ddl.DataSource = DTParent;
        ddl.DataTextField = "GNAME";
        ddl.DataValueField = "GID";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("--Select Parent Group--", "-1"));
    }
    protected void Reset_Control()
    {
        txtGroup.Text = string.Empty;
        txtAltGroup.Text  = string.Empty;
        ddlParentGroup.SelectedValue = "-1";
    }

    protected void _BindGridViewData()
    {
        DataTable DTGroup = DU.getdatausingprocedure("USP_ACC_GetGroups");
        grdDisplay.DataSource = DTGroup;
        grdDisplay.DataBind();
        custPager.TotalPages = 2;
    }
    protected void grdDisplay_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        _BindGridViewData();

    }
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strPreDefined = e.Row.Cells[4].Text;
            if (strPreDefined == "True")
            {
                e.Row.Cells[5].Controls[0].Visible = false;
            }
            if (e.Row.RowIndex == grdDisplay.EditIndex)
            {
                HiddenField hidPID = e.Row.Cells[3].FindControl("HidPid") as HiddenField;
                DropDownList ddlEditParentGroup = e.Row.Cells[3].FindControl("ddlEditParent") as DropDownList;                
                _BindParentGroup(ddlEditParentGroup);
                ddlEditParentGroup.SelectedValue = hidPID.Value;
            }        
        }
    }
    protected void grdDisplay_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {        
        DataKey dk = grdDisplay.DataKeys[e.RowIndex];
        TextBox txtEditGname = (TextBox)grdDisplay.Rows[e.RowIndex].Cells[1].FindControl("txtEditGname");
        TextBox txtEditGAname = (TextBox)grdDisplay.Rows[e.RowIndex].Cells[2].FindControl("txtEditAltGname");
        DropDownList ddlEditParentGroup = (DropDownList)grdDisplay.Rows[e.RowIndex].Cells[3].FindControl("ddlEditParent");
        _SaveGroup(dk.Value.ToString(), txtEditGname.Text, txtEditGAname.Text, ddlEditParentGroup.SelectedValue);
        grdDisplay.EditIndex = -1;
        _BindGridViewData();

    }
    protected void grdDisplay_RowEditing(object sender, GridViewEditEventArgs e)
    {
        lblMsg.Text = string.Empty;
        grdDisplay.EditIndex = e.NewEditIndex;
        _BindGridViewData();
    }
    protected void grdDisplay_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        lblMsg.Text = string.Empty;
        grdDisplay.EditIndex = -1;
        _BindGridViewData();
    }

    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        _BindGridViewData();
    }

    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");                
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");                
            }
        }
    }


    private bool _ValidateGroup1(string strGID,string strGAName)
    {
        strsql = "Select GID from ACC_GRPMAST Where (GANAME='" + strGAName + "' OR GNAME='" + strGAName + "')AND GANAME<>'' AND GID<>'" + strGID + "'";
        DataTable DTGName = DU.getdata(strsql);
        if (DTGName.Rows.Count > 0)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Duplicate Group Name or Alternate Name!";             
            Response.Write(strsql);
            return false;
        }
        else
        {
            return true;
        }

    }

    private bool _ValidateGroup(string strGID,string strGname)
    {
        strsql = "Select GID from ACC_GRPMAST Where (GANAME='" + strGname + "' OR GNAME='" + strGname + "') AND GID<>'" + strGID + "'";
        DataTable DTGName = DU.getdata(strsql);
        if (DTGName.Rows.Count > 0)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Duplicate Group Name or Alternate Name!";            
            return false;
        }
        else
        {
            return true;
        }

    }
}
